import './index.scss';
import CONSTANT from '@/common/constants.js';
import http from '@/common/http.js';
import Group from 'vux/src/components/group/index.vue'
import XInput from 'vux/src/components/x-input/index.vue'
export default {
  components: {
    Group,
    XInput
  },
  data () {
    return {
      userId: '',
      imageHttp: 'http://www.qqchou.org/a/wxinterface/file/upload',
      disabledss: true,
      list: {
        userId: '',
        headUrl: '',
        name: '',
        idCard: '',
        telephone: '',
        address: ''
      },
      sfzyanzhen: function (value) {
        let r = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
        if (r.test(value)) {
          return {
            valid: true,
            msg: '身份证正确'
          }
        } else {
          return {
            valid: false,
            msg: '身份证错误'
          }
        }
      }
    }
  },
  mounted () {
    if (this.$emit('ifOpenid')) {
      this.$emit('ifOpenid', this.$route.path)
    }
    this.userId = window.sessionStorage.getItem('userId')
    this.list.userId = this.userId
    http.post(CONSTANT.WECHATUSER, {userId: this.userId}, (res) => {
      console.log(res)
      if (res.code == 200) {
        if (res.obj.idCard == '') {
          this.disabledss = false
        }
        this.list.headUrl = res.obj.headUrl
        this.list.name = res.obj.name
        this.list.idCard = res.obj.idCard
        this.list.telephone = res.obj.telephone
        this.list.address = res.obj.address
        if (this.list.headUrl == '') {
          this.list.headUrl = window.sessionStorage.getItem('wxAvatar')
        }
        // if (this.list.name == '') {
        //   this.list.name = window.sessionStorage.getItem('nickName')
        // }
        console.log(this.list.headUrl)
        console.log(this.imageUrl.imageUrl + res.url)
      }
    })
  },
  methods: {
    submit () {
      if (this.list.headUrl == '') {
        alert('请上传头像！')
      } else if (this.list.name == '') {
        alert('请填写姓名！')
      } else if (this.list.idCard == '') {
        alert('请填写身份证号码！')
      } else if (this.$refs.sfz.valid == false) {
        alert('请填写正确的身份证号码！')
      } else if (this.list.telephone == '') {
        alert('请填写手机号码！')
      } else {
        http.post(CONSTANT.USER_EDIT, this.list, (res) => {
          console.log(res)
          if (res.code == 200) {
            alert('保存成功！')
            window.history.go(-1)
          }
        })
      }
    },
    handleAvatarSuccessz(res, file) {
      console.log(res)
      this.list.headUrl = res.url
    },
    beforeAvatarUpload(file) {
      console.log('上传之前')
    },
    handleAvatarError () {
      alert('上传失败,请重新上传！')
    },
    handleAvatarPorgress () {
      console.log('正在上传')
    },
    comeBack () {
      window.history.back()
    }
  }
}
